/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sa4project1

import java.awt.BorderLayout
import java.io.File
import java.net.URL;
import scala.xml._

import parsing._
import scala.collection.mutable
import javax.imageio.ImageIO



object Main {


  
  def main(args: Array[String]): Unit = {
    // List(1,2,3,4)
    val query = args(0)
    val twitter = "http://search.twitter.com/search.atom?q=<" + query + ">"
    val url = new URL(twitter)
    val conn = url.openConnection
    val xml = XML.load(conn.getInputStream)
    val filter = xml \\ "entry"
    var how_many_tweet = 0
    val authors = mutable.Set("","")
    authors.clear

    for(res <- filter){
      var title = res \\ "title"
      var author = res \\ "author" \\ "name"
      if(!authors.contains(author.text)){
        authors.add(author.text)
      }
      
      var published = res \\ "published"
      var geo = res \\ "georss:point"

    //Google Maps Generator
      var greq = "http://maps.google.com/maps/api/staticmap?center=Brooklyn+Bridge,New+York,NY&zoom=14&size=512x512&maptype=roadmap&markers=color:blue|label:S|40.702147,-74.015794&markers=color:green|label:G|40.711614,-74.012318&markers=color:red|color:red|label:C|40.718217,-73.998284&sensor=false"
      val gurl = new URL(greq)
      val image = ImageIO.read(gurl)
      val file = new File("query.png")
      ImageIO.write(image, "png", file)
//      val frame = new JFrame();
//      val label = new JLabel(new ImageIcon(image));
//      frame.getContentPane().add(label, BorderLayout.CENTER);
//      frame.pack();
//      // frame.setVisible(true);


      println(published.text)
      println(author.text)
      println(title.text)
      println(geo.text)
      
      
      how_many_tweet = how_many_tweet + 1
    }
      
    println("The query " + query + " matched " + how_many_tweet + " tweets by " + authors.size + " users.")
  }

}
